#include "Com_tcp.h"
#include "Com_format.h"

/**
 * @msg: 获取 客户端 通信套接字
 * @param {char} *ip
 * @param {int} port
 * @return {*} 成功返回通信套接字，失败返回-1
 */
int getSocketfd(const char *ip, int port)
{
    //socket
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(-1 == sockfd)
    {
        perror("socket");
        return -1;
    }
    //connect
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = inet_addr(ip);
    socklen_t len = sizeof(addr);
    if(-1 == connect(sockfd,(struct sockaddr*)&addr,len))
    {
        perror("connect");
        return -1;
    }

    //返回连接套接字
    return sockfd;
}
